/*
 * @Descripttion: 
 * @version: 
 * @Author: turingchang
 * @Date: 2020-12-22 17:11:58
 * @LastEditors: turingchang
 * @LastEditTime: 2020-12-25 14:29:06
 */

'use strict';

const Service = require('egg').Service;
const { Op } = require("sequelize");

class JobGroupService extends Service {

    /**
     * 查询计划任务组
     * @param {*}} params :{id:id,pid:pid,name:name}
     */
    async get(params){
        const ctx =this.ctx;
        let res = {
            success:false,
            msg:'',
            response_code_enum:null,
        }      
        let job_group ={};
        let where ={};  
        if(!params){
            res.response_code_enum = ctx.app.enums.response_code_enum.miss_required_params;
            return job_group;
        }
        if(params.id && ctx.app.utils.validator.isNum(params.id)){
            where.id =params.id;
        }
        if(params.pid && ctx.app.utils.validator.isNum(params.pid)){
            where.pid =params.pid;
        }
        if(params.name){
            where.name ={
                [Op.like]: `%${params.name}%`
            }
        }
        where.delete_time =null;
        job_group = await this.ctx.model.Job.JobGroup.findOne({
            attributes: { exclude: ['delete_time']},
            where:where,
        });
        return job_group;
    }

    /**
     * 添加计划任务组.post
     */
    async add(params){
        const ctx =this.ctx;
        let job_group ={};
        let res = {
            success:false,
            msg:'',
            response_code_enum:null,
        }        
        if(!params){
            res.response_code_enum = ctx.app.enums.response_code_enum.miss_required_params;
            return res;
        }
        let validator_result = await ctx.validate('job.jobGroup.add', params); 
        if (!validator_result) {
            res.response_code_enum = ctx.app.enums.response_code_enum.param_error;
            res.msg = JSON.stringify(validator_result);
            return res;
        }
        
        if(params.pid && ctx.app.utils.validator.isNum(params.pid)){
            job_group = await this.get({
                id:params.pid
            });
            if(job_group && job_group.id>0){
                params.pid = job_group.id;
            }else{
                params.pid = 0;
            }
        }
        params.pid = params.pid || 0 ;

        if(params.name){
            job_group = await this.get({
                name:params.name,
                pid:params.pid,
            });
            if(job_group && job_group.id>0){
                res.response_code_enum = ctx.app.enums.response_code_enum.param_error;
                res.msg = "该名称已存在";
                return res;
            }
        }

        params.uuid = ctx.app.utils.generator.uid();
        let save_result = await ctx.app.model.Job.JobGroup.create(params);
        if(!save_result){
            res.msg = "添加失败";
            res.response_code_enum = ctx.app.enums.response_code_enum.server_error;
            return res;
        }
        res.msg = "添加成功";
        res.success =true;
        return res;
    }
        
    /**
     * 编辑计划任务组
     */
    async edit(params){
        const ctx =this.ctx;
        let job_group ={};
        let res = {
            success:false,
            msg:'',
            response_code_enum:null,
        }  
        if(!params){
            res.response_code_enum = ctx.app.enums.response_code_enum.miss_required_params;
            return res;
        }
        if(!params.id){
            res.response_code_enum = ctx.app.enums.response_code_enum.miss_required_params;
            return res;
        }

        if(params.name){
            job_group = await this.ctx.model.Job.JobGroup.findOne({
                attributes: { exclude: ['delete_time']},
                where:{
                    name:params.name,
                    id:{
                        [Op.ne]: params.id,
                    }
                },
            });
            if(job_group && job_group.id>0){
                res.response_code_enum = ctx.app.enums.response_code_enum.param_error;
                res.msg = "该名称已存在，换一个吧";
                return res;
            }
        }
        
        var d = await this.ctx.model.Job.JobGroup.findByPk(params.id);
        if (!d) {
            res.response_code_enum = ctx.app.enums.response_code_enum.param_error;
            res.msg = "操作的数据不存在";
            return res;
        }
        d = Object.assign(d,params);
        try {
            await d.save();            
        } catch (e) {
            res.response_code_enum = ctx.app.enums.response_code_enum.server_error;
            res.msg = "编辑失败";
            return res;
        }   
        res.response_code_enum = ctx.app.enums.response_code_enum.success;
        res.msg = "编辑成功";     
        res.success =true;
        return res;
    }

    /**
     * 初始化默认分组
     */
    async initDefaultGroup(){
        const ctx =this.ctx;
        let job_group = {};
        job_group = await this.get({
            pid:0,
            name:'default'
        });
        if(job_group && job_group.id>0){
            return;
        }
        job_group = {};
        job_group.pid =0;
        job_group.name = 'default';
        job_group.uuid = ctx.app.utils.generator.uid();
        job_group.tag = '系统默认分组';
        let save_result = await ctx.app.model.Job.JobGroup.create(job_group);
        return;
    }    
    
  
}

module.exports = JobGroupService;